package com.das.mechanic_base.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.maintain.MainTainPlanBean;
import com.das.mechanic_base.mvp.view.camera.X3HomeCameraActivity;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.ArrayList;
import java.util.List;

import static com.das.mechanic_base.app.X3Constent.CODE_CAMERA_VIN_REQUEST;

public class X3RouterCenter {

    public static void startNavigate(String path, Bundle bundle) {
        Postcard build = ARouter.getInstance().build(path).withTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        if (bundle == null) {
            build.navigation();
            return;
        }
        build.with(bundle).navigation();
    }

    public static void startNavigateWithResult(Context mContext,String path, Bundle bundle,int requestCode) {
        Postcard build = ARouter.getInstance().build(path).withTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        if (bundle == null) {
            build.navigation();
            return;
        }
        build.with(bundle).navigation((Activity) mContext,requestCode);
    }

    /**
     * 跳转到接车单列表
     */
    public static void startReceiveOrder(long receiveBaseId,long carId,boolean detectionFirstOrNot,boolean isCreate) {
        Bundle bundle = new Bundle();
        bundle.putLong("receiveBaseId", receiveBaseId);
        bundle.putLong("carId", carId);
        bundle.putBoolean("detectionFirstOrNot", detectionFirstOrNot);
        bundle.putBoolean("isCreate",isCreate);
        bundle.putString("title","接车单详情");
        startNavigate(X3AppRouterConstants.Router.SERVICE_INFO,bundle);
    }

    /**
     * 跳转到保养计划列表
     */
    public static void startOriginFactoryPlan(long carId, ArrayList<MainTainPlanBean> bean) {
        Bundle bundle = new Bundle();
        bundle.putLong("carId", carId);
        bundle.putString("title","上次保养时间");
        bundle.putParcelableArrayList("beanList",bean);
        startNavigate(X3AppRouterConstants.Router.MAINTAIN_PLANE,bundle);
    }

    public static void startVin(Context mContext) {
        boolean isSdk = (boolean) SpHelper.getData(X3Constent.IS_OPEN_SDK, false);
        XXPermissions.with(mContext)
                .permission(Permission.CAMERA)
                .permission(Permission.Group.STORAGE)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if(all){
                            if (!X3Utils.isFastClick()) {
                                return;
                            }
                            String path = X3FileUtils.getStoragePath(mContext, X3FileUtils.MECHANIC_PHOTO_STORAGE_PATH) + "work_tain" + System.currentTimeMillis() + ".jpg";
                            if(X3Constent.isChina() && !isSdk){
                                Bundle bundle = new Bundle();
                                bundle.putString("path",path);
                                bundle.putString("title", mContext.getString(R.string.x3_recognize_vin));
                                X3RouterCenter.startNavigateWithResult(mContext,X3AppRouterConstants.Router.HOME_VIN,bundle,CODE_CAMERA_VIN_REQUEST);

                            } else {
                                Intent intent = new Intent(mContext, X3HomeCameraActivity.class);
                                intent.putExtra("path", path);
                                intent.putExtra("title", mContext.getString(R.string.x3_recognize_vin));
                                ((Activity)mContext).startActivityForResult(intent, CODE_CAMERA_VIN_REQUEST);
                            }

                        }else {
                            XXPermissions.startPermissionActivity(mContext,permissions);
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        XXPermissions.startPermissionActivity(mContext,permissions);
                    }
                });
    }
}
